package com.nsu.tly.intercept;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Description:
 * @Author: Elder Tang
 * @Date: 2021/7/21 9:43
 */
public class LoginInterceptor1 implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取用户请求的URI地址  用户判断是否为登录请求
        String uri=request.getRequestURI();
        if((uri.indexOf("/login")>0) || (uri.indexOf("/static")>0)){
            return true;
        }
        HttpSession session=request.getSession();
        String user= (String) session.getAttribute("user");
        if(user!=null){
            return true;
        }else{
            request.setAttribute("msg","你还没有登录，请先登录再操作！");
            request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
